package com.ht.api.call.func.impl.status;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.sql.DataSource;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.Entity;
import com.ht.api.db.exception.HtException;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 旅程阶段状态变更
 * @author asq
 * @createTime 2025年3月12日 13:31:46
 */
public class UpdateStageStatusFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "旅程阶段状态变更";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		// 获取登陆用户的数据源
		DataSource ds = me.getDataSource();

        String sopId = param.getStr("sop_id");
		String stageId = param.getStr("main_id");
        String planId = param.getStr("plan_id");
        String campaignId = param.getStr("campaign_id");
        int status = param.getInt("status");

        List<Entity> dataList = new ArrayList<>();

        String time = DateUtil.now();
        if (status == -1) {
            Entity plan = Entity.create("ad_plan");
            plan.set("main_id", planId);
            plan.set("status", 3);
            dataList.add(plan);

            //停用阶段，需要将阶段用户的广告任务全部取消
            DbList taskList = Db.createSqlParser("ad_user_task").select("main_id").where("campaign_id", campaignId).where("status",1).query(ds);
            for (Entity task : taskList) {
                task.set("status", 4);
                task.set("result_msg", "因阶段停用，取消任务");
                dataList.add(task);
            }

            DbList userList = Db.createSqlParser("ad_plan_user").select("main_id,oneid").where("source_id", stageId).where("status","in", Arrays.asList(1,2)).query(ds);
            for (Entity user : userList) {
                user.set("status", 4);
                dataList.add(user);

                Entity adPlanUserLogs = Entity.create("ad_plan_user_logs");
                adPlanUserLogs.set("oneid", user.get("oneid"));
                adPlanUserLogs.set("plan_id", plan.getMainId());
                adPlanUserLogs.set("time", time);
                adPlanUserLogs.set("status", 4);
                dataList.add(adPlanUserLogs);
            }

        } else if (status == 1) {
            long count = Db.createSqlParser("ad_campaign_item").where("campaign_id", campaignId).count(ds);
            if (count == 0) {
                throw new HtException("未创建广告策略，不能启动旅程阶段！");
            }

            Integer sopStatus = Db.createSqlParser("sop").select("status").where("main_id", sopId).val(ds);
            if (sopStatus != null && sopStatus == -1) {
                throw new HtException("旅程在暂停中，不能启动阶段！");
            }

            Entity plan = Entity.create("ad_plan");
            plan.set("main_id", planId);
            plan.set("status", 2);
            plan.set("start_time", DateUtil.now());
            dataList.add(plan);
        }

        Entity stage = Entity.create("stage");
        stage.set("main_id", stageId);
        stage.set("status", status);

        dataList.add(stage);

        Db.insertBatchWithStream(me.getStreamLoadConfig(), dataList);

		return JSONUtil.createObj();
	}

}
